﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace WindowsFormsControlLibrary1
{

    public enum suhu { Fahrenheit, Celcius , Kelvin};
    public enum Style {  Compact , Strecth }
    public partial class EasyWeather : UserControl
    {
        private Dictionary<int, string> map = new Dictionary<int, string>();
        private Dictionary<string, string> webKota = new Dictionary<string, string>();
        private static int[] suhu = new int[3];
        private suhu selectedDerajat;
        private Style currentStyle;
        private bool flagChange =  false;
        private int selectedIndexKota;
        
        public EasyWeather()
        {
            InitializeComponent();
            generateIndex();
            selectedIndexKota = -1;
            comboBoxKota.SelectedIndex = 0;
            comboBoxKota_SelectedIndexChanged(null, null);
        }

        //Generate Index
        private void generateIndex()
        {
            map[0] = "Ambon";
            map[1] = "Banda Aceh";
            map[2] = "Bandung";
            map[3] = "Banjarmasin";
            map[4] = "Bengkulu";
            map[5] = "Denpasar";
            map[6] = "Gorontalo";
            map[7] = "Jakarta";
            map[8] = "Jambi";
            map[9] = "Jayapura";
            map[10] = "Kupang";
            map[11] = "Makassar";
            map[12] = "Mamuju";
            map[13] = "Manado";
            map[14] = "Mataram";
            map[15] = "Medan";
            map[16] = "Padang";
            map[17] = "Palangka Raya";
            map[18] = "Palu";
            map[19] = "Pangkal Pinang";
            map[20] = "Pekanbaru";
            map[21] = "Pontianak";
            map[22] = "Samarinda";
            map[23] = "Semarang";
            map[24] = "Serang";
            map[25] = "Surabaya";
            map[26] = "Tanjung Pinang";
            map[27] = "Yogyakarta";
            webKota[map[0]] = "http://www.weather.com/weather/today/Ambon+Indonesia+IDXX0002";
            webKota[map[1]] = "http://www.weather.com/weather/today/Banda%20Aceh+Indonesia+IDXX1960";
            webKota[map[2]] = "http://www.weather.com/weather/today/Bandung+Indonesia+IDXX0007";
            webKota[map[3]] = "http://www.weather.com/weather/today/Banjarmasin+Indonesia+IDXX0009";
            webKota[map[4]] = "http://www.weather.com/weather/today/Bengkulu+Indonesia+IDXX0012";
            webKota[map[5]] = "http://www.weather.com/weather/today/Denpasar+Indonesia+IDXX0019";
            webKota[map[6]] = "http://www.weather.com/weather/today/Gorontalo+Indonesia+IDXX0485";
            webKota[map[7]] = "http://www.weather.com/weather/today/Jakarta+Indonesia+IDXX0022";
            webKota[map[8]] = "http://www.weather.com/weather/today/Jambi+Indonesia+IDXX0528";
            webKota[map[9]] = "http://www.weather.com/weather/today/Jayapura/Sentani+Indonesia+IDXX0089";
            webKota[map[10]] = "http://www.weather.com/weather/today/Kupang+Indonesia+IDXX0028";
            webKota[map[11]] = "http://www.weather.com/weather/today/Makassar+Indonesia+IDXX0884";
            webKota[map[12]] = "http://www.weather.com/weather/today/Mamudju+Indonesia+IDXX0901";
            webKota[map[13]] = "http://www.weather.com/weather/today/Manado+Indonesia+IDXX0031";
            webKota[map[14]] = "http://www.weather.com/weather/today/Mataram+Indonesia+IDXX0032";
            webKota[map[15]] = "http://www.weather.com/weather/today/Medan+Indonesia+IDXX0033";
            webKota[map[16]] = "http://www.weather.com/weather/today/Padang+Indonesia+IDXX0035";
            webKota[map[17]] = "http://www.weather.com/weather/today/Palangkaraya/Panarung+Indonesia+IDXX0066";
            webKota[map[18]] = "http://www.weather.com/weather/today/Palu/Mutiara+Indonesia+IDXX0078";
            webKota[map[19]] = "http://www.weather.com/weather/today/Pangkalpinang+Indonesia+IDXX0062";
            webKota[map[20]] = "http://www.weather.com/weather/today/Pekanbaru+Indonesia+IDXX1190";
            webKota[map[21]] = "http://www.weather.com/weather/today/ Pontianak/Supadio+Indonesia+IDXX0064";
            webKota[map[22]] = "http://www.weather.com/weather/today/Samarinda+Indonesia+IDXX0045";
            webKota[map[23]] = "http://www.weather.com/weather/today/Semarang+Indonesia+IDXX0046";
            webKota[map[24]] = "http://www.weather.com/weather/today/Serang+Indonesia+IDXX0047";
            webKota[map[25]] = "http://www.weather.com/weather/today/Surabaya+Indonesia+IDXX0052";
            webKota[map[26]] = "http://www.weather.com/weather/today/Tandjung%20Pinang+Indonesia+IDXX1648";
            webKota[map[27]] = "http://www.weather.com/weather/today/Yogyakarta+Indonesia+IDXX0058";

            comboBoxKota.DataSource = new BindingSource(map, null);
            comboBoxKota.DisplayMember = "Value";
            comboBoxKota.ValueMember = "Key";


        }
        private void mainProcess(string kota)
        {
            
            string path = kota;
            HtmlAgilityPack.HtmlWeb web = new HtmlAgilityPack.HtmlWeb();
            HtmlAgilityPack.HtmlDocument a = web.Load(path);
            HtmlAgilityPack.HtmlNode suhuF = a.DocumentNode.SelectSingleNode("//div[@id='wx-inner-rail']/div[@class='wx-module wx-weather wx-current wx-grid3of6 wx-obs-pco-data wx-featured']/ul/li[@class='wx-temp']/span[@class='wx-value']");
            HtmlAgilityPack.HtmlNode cuaca = a.DocumentNode.SelectSingleNode("//div[@id='wx-inner-rail']/div[@class='wx-module wx-weather wx-current wx-grid3of6 wx-obs-pco-data wx-featured']/ul/li[@class='wx-phrase']/span[@class='wx-value']");
            HtmlAgilityPack.HtmlNode img = a.DocumentNode.SelectSingleNode("//img[@class='wx-weather-icon']");
            string url = img.Attributes["src"].Value;
            //url = url.Remove(0, 1);
            // url = @"D:\KULIAH\PBK\Webpage\webPage" + url;

            weatherPicture.ImageLocation = url;
            
            weatherName.Text = cuaca.InnerHtml;

            //variable
            suhu[0] = int.Parse(suhuF.InnerHtml);
            suhu[1] = (suhu[0] - 32) / 9 * 5;
            suhu[2] = suhu[1] + 273;
            comboBoxSuhu.SelectedIndex =(int) selectedDerajat;
            comboBoxSuhu_SelectedIndexChanged(null, null);
        }


        private void opensplash()
        {
            Application.Run(new Splash(this));
        }

        private void comboBoxKota_SelectedIndexChanged(object sender, EventArgs e)
        {
            
                timer1.Stop();
                label1.Visible = true;
                Application.DoEvents();
                mainProcess(webKota[map[comboBoxKota.SelectedIndex]]);
                label1.Visible = false;

                timer1.Start();
                selectedIndexKota = comboBoxKota.SelectedIndex;
                flagChange = true;
            
        }

        private void comboBoxSuhu_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBoxSuhu.SelectedIndex == 1)
            {

                suhuAngka.Text = "" + suhu[1] + "°";
            }
            else if (comboBoxSuhu.SelectedIndex == 0)
            {

                suhuAngka.Text = "" + suhu[0] + "°";
            }
            else if (comboBoxSuhu.SelectedIndex == 2)
            {
                suhuAngka.Text = "" + suhu[2];

            }
            selectedDerajat = (suhu)comboBoxSuhu.SelectedIndex;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            
        }

        //ChangeStyle
        private void changeStyle(int i)
        {
            if (i == 0)
            {
                // 
                // comboBoxSuhu
                // 

                this.Size = new System.Drawing.Size(235, 149);
                this.MinimumSize = new System.Drawing.Size(235, 149);
                this.MaximumSize = new System.Drawing.Size(235, 149);
                this.comboBoxSuhu.Location = new System.Drawing.Point(178, 78);
                this.comboBoxSuhu.Size = new System.Drawing.Size(48, 28);
                // 
                // weatherPicture
                // 
                this.weatherPicture.Location = new System.Drawing.Point(13, 11);
               this.weatherPicture.Size = new System.Drawing.Size(101, 95);
                this.weatherPicture.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
               // 
                // weatherName
                // 
                this.weatherName.AutoSize = true;
                this.weatherName.Font = new System.Drawing.Font("Microsoft Sans Serif", 13F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                this.weatherName.Location = new System.Drawing.Point(98, 33);
                this.weatherName.Size = new System.Drawing.Size(64, 22);
                // 
                // suhuAngka
                // 
                this.suhuAngka.AutoSize = true;
                this.suhuAngka.Font = new System.Drawing.Font("Microsoft Sans Serif", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                this.suhuAngka.Location = new System.Drawing.Point(120, 77);
                this.suhuAngka.Size = new System.Drawing.Size(52, 29);
                // 
                // comboBoxKota
                // 
               this.comboBoxKota.Location = new System.Drawing.Point(30, 112);
               this.comboBoxKota.Size = new System.Drawing.Size(179, 28);
                this.comboBoxKota.TabIndex = 4;
                // 
                // label1
                // 
                this.label1.Location = new System.Drawing.Point(161, 9);
                this.label1.Size = new System.Drawing.Size(78, 20);
                // 
                // UserControl1
                // 
   
            
            }
            if (i == 1)
            {
                
                this.MinimumSize = new System.Drawing.Size(0, 0);
                this.MaximumSize = new System.Drawing.Size(420, 40);
                this.Size = new System.Drawing.Size(420, 40);
                // 
                // pictureBox1
                // 
                this.weatherPicture.Location = new System.Drawing.Point(4, 4);
                this.weatherPicture.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);
                this.weatherPicture.Size = new System.Drawing.Size(30, 30);
                this.weatherPicture.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
                

                // 
                // suhuAngka
                // 
                this.suhuAngka.AutoSize = true;
                this.suhuAngka.Font = new System.Drawing.Font("Microsoft Sans Serif", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                this.suhuAngka.Location = new System.Drawing.Point(33, 7);
                this.suhuAngka.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
                
                this.suhuAngka.Size = new System.Drawing.Size(40, 20);
    
                // 
                // comboBoxSuhu
                // 
                this.comboBoxSuhu.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
                this.comboBoxSuhu.FormattingEnabled = true;
                this.comboBoxSuhu.Location = new System.Drawing.Point(79, 8);
                this.comboBoxSuhu.Size = new System.Drawing.Size(40, 30);
               
                // 
                // weatherName
                // 
                this.weatherName.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                this.weatherName.Location = new System.Drawing.Point(128, 8);
                this.weatherName.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
                this.weatherName.Size = new System.Drawing.Size(133, 49);
              
                // 
                // comboBoxKota

                this.comboBoxKota.Location = new System.Drawing.Point(240, 8);
                this.comboBoxKota.Size = new System.Drawing.Size(174, 28);
            }
        
        }



        //
        // PROPERTY IMAGE
        //
      


        public Bitmap fweatherPicture {
            get {

                return (Bitmap)weatherPicture.Image;
            }
        
        }


        public string fweatherName {
            get {
                return weatherName.Text;
            }
        }

        public string getNamaKota
        {
            get { 
                return map[comboBoxKota.SelectedIndex];
            }
        }

        public int setIndexKota
        {
            set {
                comboBoxKota.SelectedIndex = value;
                comboBoxKota_SelectedIndexChanged(null, null);
            }
        }

        public Dictionary<int, string> getDictionary()
        {
            

                return this.map;
            
        
        }


        public string getSuhu {
            get {

                return suhuAngka.Text;
            }
        }

        public string getSuhuInCelcius {
            get {
                return suhu[1] + "°";
            
            }
        }

        public string getSuhuInFahrenheit {
            get {
                return suhu[0] + "°";
            }
        }

        public string getSuhuInKelvin {
            get {
                return suhu[2] + "°";
            }
        }


        public suhu Derajat {
            get {
                return this.selectedDerajat;
            }
            set {
                this.selectedDerajat = value;
                comboBoxSuhu.SelectedIndex = (int)value;
                comboBoxSuhu_SelectedIndexChanged(null,null);
            }
        
        }


        public Style setStyle
        {
            get {
                return this.currentStyle;
            }
            set {
                this.currentStyle = value;
                changeStyle((int)value);
            }
        }

        public System.Windows.Forms.ComboBoxStyle comboBoxStyle
        {
            get {
                return comboBoxKota.DropDownStyle;
            
            }
            set {
                if (value == System.Windows.Forms.ComboBoxStyle.Simple)
                {
                    comboBoxKota.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDown;
                    comboBoxSuhu.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDown;
                }
                comboBoxKota.DropDownStyle = value;
                comboBoxSuhu.DropDownStyle = value;
            
            }
            
        }

        public bool isChange
        {
            get {
                bool a = flagChange;
                this.flagChange = false;
                return a;
                
            }
        
        }

        //Update
        private void timer1_Tick(object sender, EventArgs e)
        {
            mainProcess(webKota[map[comboBoxKota.SelectedIndex]]);
            flagChange = true;
        }
       

    }
}
